package org.zzh.springboot.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.zzh.springboot.entity.User;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface UserMapper extends Mapper<User> {
    //登录功能-用户名和密码
    @Select("select * from m_user where name=#{name} and password=#{password}")
    User login(@Param("name") String name,@Param("password")String password);
    // 模糊查询方法（根据用户名模糊匹配）
//    @Select("SELECT * FROM m_user WHERE name LIKE CONCAT('%', #{keyword}, '%')")
//    List<User> searchUsersByName(@Param("keyword") String keyword);
    // 模糊删除（根据用户名模糊匹配）
    @Delete("DELETE FROM m_user WHERE name LIKE CONCAT('%', #{name}, '%')")
    int deleteByUsername(@Param("name") String name);
    //多条件查询
    @Select({
            "<script>",
            "SELECT * FROM m_user",
            "<where>",
            "<if test='name != null and name != \"\"'> AND name LIKE CONCAT('%', #{name}, '%') </if>",
            "<if test='sex != null and sex != \"\"'> AND sex = #{sex} </if>",
            "<if test='address != null and address != \"\"'> AND address LIKE CONCAT('%', #{address}, '%') </if>",
            "</where>",
            "</script>"
    })
    List<User> searchUsersByConditions(
            @Param("name") String name,
            @Param("sex") String sex,
            @Param("address") String address
    );
}
